/*
 * Copyright 2004 by Shmulik London. All rights reserved.
 *
 * Redistribution and use of this source code, with or without modification, 
 * are permitted for commercial or uncommercial use, provided that it retains 
 * the above copyright notice and the following disclaimer.
 *
 * This source code was developed to support the course 'Enterprise Application
 * Development using J2EE' given in the Interdisciplinary Center Herzelia in 
 * Winter 2004 by Shmulik London.
 * 
 * This software is provided "AS IS," without a warranty of any
 * kind. THE AUTHOR OF THIS SOFTWARE MAKES NO REPRESENTATIONS OR WARRANTIES 
 * ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING 
 * BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR SHALL NOT BE LIABLE FOR 
 * ANY DAMAGES SUFFERED AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
 * THIS SOFTWARE OR ITS DERIVATIVES.
 */
package web;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 * An HttpServlet that generates a page which displays a random
 * color table
 *
 * @author Shmulik London
 */
public class ColorTableServlet extends HttpServlet {

    private static final int COLUMNS = 10;
    private static final int ROWS = 10;

    public void doGet(
        HttpServletRequest request, 
        HttpServletResponse response)
        throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter writer = response.getWriter();
        writer.println("{THIS WILL BE AWESOME JSON CODE!!!}");
        
    }

    private void generateTable(PrintWriter writer, int rowCount, int columnCount) {
        writer.println("<table border=1 width=80%>");
        for (int row = 0; row < rowCount; row++) {
            writer.println(" <tr align=center>");
            for (int column = 0; column < columnCount; column++) {
                int r = (int)(Math.random()*255);
                int g = (int)(Math.random()*255);
                int b = (int)(Math.random()*255);
                String colorCode = encodeColor(r, g, b);
                writer.println(" <td bgcolor="+colorCode+"><font color=#ffffff>"+
                    colorCode+"<font><td>");
            }
            writer.println(" </tr>");
        }
        writer.println("</table>");
    }

    private String encodeColor(int r, int g, int b) {
        StringBuffer buffer = new StringBuffer("#");
        buffer.append(Integer.toHexString(r));
        buffer.append(Integer.toHexString(g));
        buffer.append(Integer.toHexString(b));
        return buffer.toString();
    }
}